/**
 * 
 */
package info.niwota.ziplock.img;

import info.niwota.ziplock.Debug;
import info.niwota.ziplock.FileContentProvider;
import info.niwota.ziplock.FileItem;
import info.niwota.ziplock.StatusMonitor;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
public class ZipFileList implements IImageList, Serializable {
	
	private static final long serialVersionUID = 1L;

	public class MyImage implements IImage {

		private FileItem entry;
		
		private Uri imageUri;

		private Bitmap bitmap;

		public MyImage(FileItem entry) {
			this.entry = entry;
			//
			String p = entry.getName();
			this.imageUri = Uri.withAppendedPath(base, p);

			if (Debug.DEBUG) {
				Log.d(TAG, "image uri: " + imageUri);
			}
		}

		public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels) {
			return fullSizeBitmap(minSideLength, maxNumberOfPixels, false,
					false);
		}

		public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels,
				boolean rotateAsNeeded, boolean useNative) {
			if (Debug.DEBUG) {
				Log.d(TAG, "fullSizeBitmap: " + entry.getName() + " bitmap: " + bitmap);
			}
			try {
				if (bitmap != null) {
					return bitmap;
				}
				ParcelFileDescriptor fd = FileContentProvider.open(imageUri,
						"r", cache);
				bitmap = Util.makeBitmap(minSideLength, maxNumberOfPixels,
						fd, null);
				//
				return bitmap;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		public long fullSizeImageId() {
			return 0;
		}

		public Uri fullSizeImageUri() {
			return imageUri;
		}

		public long getDateTaken() {
			return 0;
		}

		public int getDegreesRotated() {
			return 0;
		}

		public String getDisplayName() {
			return null;
		}

		public int getHeight() {
			return 0;
		}

		public String getMimeType() {
			return null;
		}

		public String getTitle() {
			return null;
		}

		public int getWidth() {
			return 0;
		}

		public Bitmap miniThumbBitmap() {
			if (Debug.DEBUG) {
				Log.d(TAG, "miniThumbBitmap: " + entry.getName());
			}
			return thumbBitmap(IImage.ROTATE_AS_NEEDED);
		}

		public boolean rotateImageBy(int degrees) {
			return false;
		}

		public Bitmap thumbBitmap(boolean rotateAsNeeded) {
			return fullSizeBitmap(THUMBNAIL_TARGET_SIZE,
					THUMBNAIL_MAX_NUM_PIXELS);
		}
	}

	private static final String TAG = "ZipFileList";

	private File cache;

	private ArrayList<FileItem> list;
	
	private Uri uri;

	private Uri base;

	public ZipFileList() {
	}

	public void load(Context ctx, Uri uri, StatusMonitor status) throws Exception {
		if (list != null) {
			return;
		}
		this.uri = uri;
		this.list = new ArrayList<FileItem>();
		//
		// base uri
		String zipname = uri.getLastPathSegment();
		this.base = FileContentProvider.constructBaseUri(zipname);
		if (Debug.DEBUG) {
			Log.d(TAG, "ZipFileList uri: " + uri + " base: " + base);
		}
		// cache for enc zip
		this.cache = new File(ctx.getCacheDir(), this.getClass().getName());
		this.cache.mkdirs();
		// load
		FileContentProvider.load(uri, status, list);
	}

	public void close() {
		if (list != null) {
			list.clear();
			list = null;
		}
	}

	public int getCount() {
		return (list == null ? 0 : list.size());
	}

	public FileItem getItem(int i) {
		if (Debug.DEBUG) {
			Log.d(TAG, "getItem: " + i);
		}
		if (list == null) {
			return null;
		}
		FileItem fi = list.get(i);
		return fi;
	}
	
	public IImage getImageAt(int i) {
		if (Debug.DEBUG) {
			Log.d(TAG, "getImageAt: " + i);
		}
		if (list == null) {
			return null;
		}
		FileItem fi = list.get(i);

		if (Debug.DEBUG) {
			Log.d(TAG, "getImageAt: " + fi.getName());
		}

		return new MyImage(fi);
	}

	public boolean isEmpty() {
		return (list == null || list.size() == 0);
	}

	public boolean removeImageAt(int i) {
		return false;
	}
	
	public void saveState(Bundle b) {
		b.putParcelable("base", base);
		b.putParcelable("uri", uri);
		b.putSerializable("cache", cache);
		b.putSerializable("list", list);
	}
	
	@SuppressWarnings("unchecked")
	public void restoreState(Bundle b) {
		ArrayList<FileItem> serializable = (ArrayList<FileItem>) b.getSerializable("list");
		list = serializable;
		cache = (File) b.getSerializable("cache");
		uri = (Uri) b.getParcelable("uri");
		base = (Uri) b.getParcelable("base");
	}
}
